package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.LoginSessionUtil;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        Employee login=employeeService.login(employee);
        R<Employee> r = new R<Employee>();
        r.setCode(login == null ? 0 : 1);
        r.setMsg(login == null ? "用户名或密码错误" : "登录成功");
        r.setData(login);
        if (login != null && 0 == login.getStatus()){
            r.setCode(0);
            r.setMsg("账户已禁用!请联系管理员!");
            return  r;
        }
        if (login != null){
            //登陆成功 存入session
            request.getSession().setAttribute("empLogin",login);

            //判断是否已经登录
            if(LoginSessionUtil.sessionMap.containsKey(login.getId())){
//                LoginSessionUtil.sessionMap.get(login.getId()).removeAttribute("loginUser");
                //如果已登录则删除该session的对象
                LoginSessionUtil.sessionMap.get(login.getId()).setAttribute("empLogin","异地登录");
            }
            LoginSessionUtil.sessionMap.put(login.getId(),request.getSession());
        }
        return  r;
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<Employee> logout(HttpSession session){
        R<Employee> r = new R<Employee>();
        Employee loginUser = (Employee) session.getAttribute("empLogin");
            LoginSessionUtil.sessionMap.remove(loginUser.getId());
        session.removeAttribute("empLogin");
        r.setCode(loginUser == null ? 0 : 1);
        r.setMsg(loginUser == null ? "退出登录失败" : "已退出登录");
        return  r;
    }


//    http://localhost:8080/employee/page?page=1&pageSize=10

    @GetMapping("/page")
    public R<IPage<Employee>> getAllEmployeesByPage(Integer page,Integer pageSize,String name){
        IPage<Employee> allEmployeesByPage=employeeService.getAllEmployeesByPage(page, pageSize,name);
        R<IPage<Employee>> r = new R<IPage<Employee>>();
        r.setCode(allEmployeesByPage == null ? 0 : 1);
//        r.setMsg(allEmployeesByPage == null ? "加载失败" : "加载成功");
        r.setData(allEmployeesByPage);
        return r;
    }

    /**
     * 员工添加
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        //md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 回显（根据id获取用户信息）
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有该用户");
    }

    /**
     * 员工信息修改
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updateById(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("修改员工信息成功");
    }


}
